package com.neusoft.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.neusoft.domain.Department;
import com.neusoft.domain.paper.HireMapper;
import com.neusoft.domain.paper.HirePaperFilter;
import com.neusoft.domain.paper.New_employee_paper;
import com.neusoft.domain.paper.leave_employee_paper;
import com.neusoft.domain.paper.tf_employee;
import com.neusoft.domain.paper.dptTf_employee_paper;
import com.neusoft.domain.paper.jbTf_employee_paper;
import com.neusoft.domain.paper.leaveMapper;
import com.neusoft.service.PaperService;

@RequestMapping("/paper")
@Controller
public class Paper {
	
	@Resource(name="paperServiceImpl")
	private PaperService paperService;

	public void setPaperService(PaperService paperService) {
		this.paperService = paperService;
	}
	
	
	//session获取department
	public void setDepartment(HttpServletRequest request){
		List<Department> departments = paperService.getDepartment();
		System.out.println(departments.toString());
		request.getSession().setAttribute("departments", departments);
	}

	/*
	 * 各界面首次跳转
	 */
	@RequestMapping(value="/hirePaperView")
	public  String hirePaperView(HttpServletRequest request,HttpServletResponse response)
	{
		setDepartment(request);
		return "paper/hire-paper";
	}
	
	@RequestMapping(value="/firedPaperView")
	public  String firedPaperView(HttpServletRequest request,HttpServletResponse response)
	{
		setDepartment(request);
		return "paper/fired-paper";
	}
	
	//新聘员工报表，hirePaperFilter封装查询条件
	
	@RequestMapping(value="/getUsers",method=RequestMethod.POST)
	public  String getUsers(@Valid HirePaperFilter hirePaperFilter,BindingResult result, Model  model)
	{
		FieldError  startmsg = result.getFieldError("start");
		FieldError  endmsg = result.getFieldError("end");
		if(result.getErrorCount()>0)
		{
			if(startmsg!=null)
			{
				
				model.addAttribute("startmsg", startmsg);
				
			}
			if(endmsg!=null)
			{
				model.addAttribute("endmsg", endmsg);
				
			}
			return "paper/hire-paper";
		}
		List<HireMapper>  users=this.paperService.getUsers(hirePaperFilter);
		model.addAttribute("users", users);
		return "paper/hire-paper";
	}
	
	
	/*
	 * 离职员工报表
	 */
	
	@RequestMapping(value="/getFiredUsers",method=RequestMethod.POST)
	public  String getFiredUsers(@Valid HirePaperFilter hirePaperFilter,BindingResult result, Model  model)
	{
		FieldError  startmsg = result.getFieldError("start");
		FieldError  endmsg = result.getFieldError("end");
		if(result.getErrorCount()>0)
		{
			if(startmsg!=null)
			{
				
				model.addAttribute("startmsg", startmsg);
				
			}
			if(endmsg!=null)
			{
				model.addAttribute("endmsg", endmsg);
				
			}
			return "paper/fired-paper";
		}
		List<leaveMapper>  users=this.paperService.getFiredUsers(hirePaperFilter);
		model.addAttribute("users", users);
		return "paper/fired-paper";
	}
	
	/*
	 * 岗位调动员工信息
	 */
	@RequestMapping(value="/getJbTfUsers",method=RequestMethod.POST)
	public  String getJbTfUsers(@Valid HirePaperFilter hirePaperFilter,BindingResult result, Model  model)
	{
		FieldError  startmsg = result.getFieldError("start");
		FieldError  endmsg = result.getFieldError("end");
		if(result.getErrorCount()>0)
		{
			if(startmsg!=null)
			{
				model.addAttribute("startmsg", startmsg);
				
			}
			if(endmsg!=null)
			{
				model.addAttribute("endmsg", endmsg);
				
			}
			return "paper/job-mobilize-paper";
		}
		List<tf_employee>  users=this.paperService.getJbTfUsers(hirePaperFilter);
		model.addAttribute("users", users);
		return "paper/job-mobilize-paper";
	}
	
	/*
	 * 部门调动员工信息
	 */
	@RequestMapping(value="/getDptTfUsers",method=RequestMethod.POST)
	public  String getDptTfUsers(@Valid HirePaperFilter hirePaperFilter,BindingResult result, Model  model)
	{
		FieldError  startmsg = result.getFieldError("start");
		FieldError  endmsg = result.getFieldError("end");
		if(result.getErrorCount()>0)
		{
			if(startmsg!=null)
			{
				
				model.addAttribute("startmsg", startmsg);
				
			}
			if(endmsg!=null)
			{
				model.addAttribute("endmsg", endmsg);
				
			}
			return "paper/department-mobilize-paper";
		}
		List<tf_employee>  users=this.paperService.getDptTfUsers(hirePaperFilter);
		model.addAttribute("users", users);
		return "paper/department-mobilize-paper";
	}
	
	//处理前台传过来的日期
	@InitBinder
	public void initBinder(HttpServletRequest request,ServletRequestDataBinder binder)throws Exception { 
	  DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 
	  CustomDateEditor dateEditor = new CustomDateEditor(fmt, true); 
	  binder.registerCustomEditor(Date.class, dateEditor); 
	}
	
}
